﻿using MediatR;
using System.WXT.Domain;
using System.WXT.Domain.DTO;
using System.WXT.Infrstructre.Interface;
using System.WXT.WebAPI.Application.Command.WorkerHourCommand;

namespace System.WXT.WebAPI.Application.CommandHandlers.WorkeHourCommandHandlers
{
    public class WorkeHourShowCommandHandlers:IRequestHandler<WorkerHourShowCommand,PageDTO<WorkerHour>>
    {
        protected readonly IWorkerHourRepository workerHourRepository;

        public WorkeHourShowCommandHandlers(IWorkerHourRepository workerHourRepository)
        {
            this.workerHourRepository = workerHourRepository;
        }
        /// <summary>
        /// 工人工时显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<PageDTO<WorkerHour>> Handle(WorkerHourShowCommand request, CancellationToken cancellationToken)
        {
            PageDTO<WorkerHour> page = new PageDTO<WorkerHour>();
            var list = (await workerHourRepository.GetAll()).Where(x=>x.workerHourIsDel==0);
            if(!string.IsNullOrEmpty(request.workerHourNumbering))
            {
                list = list.Where(x => x.workerHourNumbering == request.workerHourNumbering);
            }
            if(request.workerHourState > 0)
            {
                list = list.Where(x => x.workerHourState == request.workerHourState);
            }
            page.listcount = list.Count();
            page.pagecount = (int)Math.Ceiling(list.Count() * 1.0 / request.pageSize);
            page.plist = list.OrderByDescending(x=>x.workerHourId).Skip((request.pageIndex-1)*request.pageSize).Take(request.pageSize).ToList();
            return page;

        }
    }
}
